package com.LLL;

import java.sql.*;

public class DBUtill {
    static {
        //注册驱动
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("数据库驱动加载失败");
        }
    }

    //获取数据库连接
    // static静态的  private私自的
    private static String url = "jdbc:mysql://121.196.166.117:3306/estoredbs?useSSL=false";
    //获取mysql的端口号
    private static String user = "root";
    private static String password = "jisuanji2022";

    //获取数据库的密码
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接异常");
        }
        return conn;
    }

    //关闭资源
    //Statement获取数据库操作对象    ResultSet执行SQL获取结果
    public static void clossResource(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                if (!rs.isClosed()) {
                    rs.close();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (stmt != null) {
                if (!stmt.isClosed()) {
                    stmt.close();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                if (!conn.isClosed()) {
                    conn.close();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
